package com.sky.utils;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
@Data

@Slf4j
public class MinioUtil {

    private String minioEndpoint;

    private String bucketName;

    private MinioClient minioClient;

    public MinioUtil(String minioEndpoint, String bucketName, MinioClient minioClient) {
        this.minioEndpoint = minioEndpoint;
        this.bucketName = bucketName;
        this.minioClient = minioClient;
    }

    public String upload(MultipartFile file, String objectName) {
        try {
            PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucketName).object(objectName)
                    .stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build();
            minioClient.putObject(objectArgs);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return minioEndpoint +
                "/" +
                bucketName +
                "/" +
                objectName;

    }


}
